package com.leetcode.LC;

public class LC905 {
    public int[] sortArrayByParity(int[] A) {

        int i = 0, j = A.length - 1;
        while (i < j) {
            if (A[i] % 2 == 0 && A[j] % 2 == 1) {
                i++;
                j--;
            } else if (A[i] % 2 == 1 && A[j] % 2 == 0) {
                int tmp;
                tmp = A[i];
                A[i] = A[j];
                A[j] = tmp;
            } else if (A[i] % 2 == 0 && A[j] % 2 == 0) {
                i++;
            } else if (A[i] % 2 == 1 && A[j] % 2 == 1) {
                j--;
            }
        }
        return A;
    }

    public static void main(String[] args) {
        for (int i : new LC905().sortArrayByParity(new int[]{3, 1})) {
            System.out.print(i + ",");
        }
    }
}
